﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;

namespace ProviderLogging
{
    class RoleLog : RoleProvider
    {
        public RoleLog()
        {
            Logging.WriteString("RoleLog started");
        }

        protected RoleProvider internalRoleProvider = null;
        protected RoleProvider InternalRoleProvider
        {
            get
            {
                if (internalRoleProvider == null) internalRoleProvider = GetInternalProvider();
                return internalRoleProvider;
            }
        }

        public const string cnstinternalProviderName = "internalProviderName";

        public override void Initialize(string name, NameValueCollection config)
        {
            _name = name;
            InternalProviderName = config[cnstinternalProviderName];
            Logging.LogOperation("Initialize", new object[] { name, config }, null, null);
            return;
        }

        protected string _internalProviderName = null;
        public string InternalProviderName
        {
            get { return (_internalProviderName); }
            set { _internalProviderName = value; }
        }

        protected string _name = null;
        public override string Name
        {
            get { return _name; }
        }

        protected RoleProvider GetInternalProvider()
        {
            RoleProvider result = null;
            try
            {
                result = Roles.Providers[InternalProviderName];

                RoleManagerSection sect = (RoleManagerSection)ConfigurationManager.GetSection("system.web/roleManager");
                ProviderSettings settings = sect.Providers[InternalProviderName];
                result.Initialize(InternalProviderName, settings.Parameters);
            }
            catch (InvalidOperationException)
            {
                // Telling us that the initialize has already been called on the other role provider -- so ignore
            }
            catch (IndexOutOfRangeException)
            {
                Logging.WriteString("Initialized failed. Unable to locate the provider.");
                throw;
            }
            catch (Exception excpt)
            {
                Logging.WriteString("GetInternalProvider Exception " + excpt.ToString());
                throw;
            }

            return result;

        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            try
            {
                InternalRoleProvider.AddUsersToRoles(usernames, roleNames);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("AddUsersToRoles", new object[] { usernames, roleNames }, null, excpt);
                throw;
            }
            Logging.LogOperation("AddUsersToRoles", new object[] { usernames, roleNames }, null, null);
            return;
        }

        public override string ApplicationName
        {
            get
            {
                string result = null;
                try
                {
                    result = InternalRoleProvider.ApplicationName;
                }
                catch (Exception excpt)
                {
                    Logging.LogOperation("ApplicationName!Get", null, null, excpt);
                    throw;
                }
                Logging.LogOperation("ApplicationName!Get", null, result, null);
                return (result);
            }
            set
            {
                try
                {
                    InternalRoleProvider.ApplicationName = value;
                }
                catch (Exception excpt)
                {
                    Logging.LogOperation("ApplicationName!Set", new object[] { value }, null, excpt);
                    throw;
                }
                return;
            }
        }

        public override void CreateRole(string roleName)
        {
            try
            {
                InternalRoleProvider.CreateRole(roleName);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("CreateRole", new object[] { roleName }, null, excpt);
                throw;
            }
            Logging.LogOperation("CreateRole", new object[] { roleName }, null, null);
            return;
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            bool result = false;
            try
            {
                result = InternalRoleProvider.DeleteRole(roleName, throwOnPopulatedRole);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("DeleteRole", new object[] { roleName, throwOnPopulatedRole }, null, excpt);
                throw;
            }
            Logging.LogOperation("DeleteRole", new object[] { roleName, throwOnPopulatedRole }, result, null);
            return result;
        }

        public override string Description
        {
            get
            {
                string result = null;
                try
                {
                    result = InternalRoleProvider.Description;
                }
                catch (Exception excpt)
                {
                    Logging.LogOperation("Description!Get", null, null, excpt);
                    throw;
                }
                Logging.LogOperation("Description!Get", null, result, null);
                return result;
            }
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            string[] result = null;
            try
            {
                result = InternalRoleProvider.FindUsersInRole(roleName, usernameToMatch);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("FindUsersInRole", new object[] { roleName, usernameToMatch }, null, excpt);
                throw;
            }
            Logging.LogOperation("FindUsersInRole", new object[] { roleName, usernameToMatch }, result, null);
            return result;
        }

        public override string[] GetAllRoles()
        {
            string[] result = null;
            try
            {
                result = InternalRoleProvider.GetAllRoles();
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("GetAllRoles", null, null, excpt);
                throw;
            }
            Logging.LogOperation("GetAllRoles", null, result, null);
            return result;
        }

        public override string[] GetRolesForUser(string username)
        {
            string[] result = null;
            try
            {
                result = InternalRoleProvider.GetRolesForUser(username);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("GetRolesForUser", new object[] { username }, null, excpt);
                throw;
            }
            Logging.LogOperation("GetRolesForUser", new object[] { username }, result, null);
            return result;
        }

        public override string[] GetUsersInRole(string roleName)
        {
            string[] result = null;
            try
            {
                result = InternalRoleProvider.GetUsersInRole(roleName);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("GetUsersInRole", new object[] { roleName }, null, excpt);
                throw;
            }
            Logging.LogOperation("GetUsersInRole", new object[] { roleName }, result, null);
            return (result);
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            bool result = false;
            try
            {
                result = InternalRoleProvider.IsUserInRole(username, roleName);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("IsUserInRole", new object[] { username, roleName }, null, excpt);
                throw;
            }
            Logging.LogOperation("IsUserInRole", new object[] { username, roleName }, result, null);
            return (result);
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            try
            {
                InternalRoleProvider.RemoveUsersFromRoles(usernames, roleNames);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("RemoveUsersFromRoles", new object[] { usernames, roleNames }, null, excpt);
                throw;
            }
            Logging.LogOperation("RemoveUsersFromRoles", new object[] { usernames, roleNames }, null, null);
            return;
        }

        public override bool RoleExists(string roleName)
        {
            bool result = false;
            try
            {
                result = InternalRoleProvider.RoleExists(roleName);
            }
            catch (Exception excpt)
            {
                Logging.LogOperation("RoleExists", new object[] { roleName }, null, excpt);
                throw;
            }
            Logging.LogOperation("RoleExists", new object[] { roleName }, result, null);
            return result;
        }
    }
}
